// import { mapState, mapMutations } from "vuex";
import cTitle from "components/title";
import { Toast } from "vant";
// var haveShop = false;

export default {
  data() {
    return {
    merchant_offer_discounts:{enable:0,diy_name:""},
      imgHost: window.location.origin,
      frontend_goods_manager:0,
      show_main_color_flag:false,
      show_store_balance_transfer: false,
      store_state:0,
      show:false,
      turnover_time:['今日','昨日','全部'],
      turnover_time_index:2,
      store: {
        name: "",
        thumb: "",
        validity: "", // 有效期
        store_id: ""
      },
      income: {
        money_total: 0.0,
        sure_withdraw_money: 0.0
      },
      goods_id: "", // 商品id
      status: 0,
      cloudHornStatus: false,
      zkzStatus: false,
      nearby: false,
      specify_show: 0, //门店进入页面 0门店首页 1门店商品页
      is_open_code: 0, //收款二维码是否开启 0关1开
      my_store_order: 0, // 显示我的订单选项栏
      order_survey: 0, // 显示订单概况
      show_store_balance_award: false,
      is_share_chain:0,
      is_share_chain_name:'共享链',
      show_management:false,

      details:{},
      deduct_return_name:'',
      deduct_return_open:false,
      store_income: [],
      store_screen: false
    };
  },
  methods: {
    pluginGoto(item) {
      if(!item.url) return;
      let params = {};
      let query = {};
      if(item.page_data && item.page_data) {
        let { data_url, ...othder } = item.page_data;
        query = { ...othder };
        if(data_url) {
          query.api = data_url;
        }
      }
      this.$router.push(this.fun.getUrl(item.url, params, query));
    },
    turnover_handle(i){
      this.turnover_time_index = i;
      let day_type=0;
      if(i==0){
        day_type=1;
      }else if(i==1){
        day_type = 2
      }
      $http.post('plugin.store-cashier.frontend.store.center.index.get-statistics',{day_type}).then(res=>{
        if(res.result==0){
          return this.$toast(res.msg);
        }
        this.details = res.data;
      })
    },
    goback() {
      this.$router.go(-1);
    },
    toNear() {
      this.$router.push(
        this.fun.getUrl("introduceNear", {
          store_id: this.store.store_id
        })
      );
    },
    gotoStoreGroup() {
      this.$router.push(this.fun.getUrl("o2oGroupActivitylist", {
        store_id: this.store.store_id
      }));
    },
    toMyStore() {
      console.log(this.specify_show);
      let url = "";
      if (this.specify_show == 1) {
        url = "o2oStore_v2";
      } else {
        url = "HomeSeller";
      }
      this.$router.push(
        this.fun.getUrl(url, {
          store_id: this.store.store_id,
          fromHome: 1
        })
      );
    },
    getStoreInfo() {
      var that = this;
      $http
        .get("plugin.store-cashier.frontend.store.center.index.index", {basic_info
          :1}," ")
        .then(
          (response)=> {
            if (response.result === 1) {
              let merchant_offer_discounts = response?.basic_info
              ?.plugin_setting?.merchant_offer_discounts;
              if(merchant_offer_discounts){
                this.merchant_offer_discounts = merchant_offer_discounts;
              }
              that.store = response.data.store;
              this.store_income = response.data.store_income;
              this.frontend_goods_manager = response.data.frontend_goods_manager;
              // that.store.validity = that.store.validity;
              if (response.data.store.validity == 0) {
                that.store.validity = "无限期";
              } else {
                // 不需要转化了，任务编号: 39431
                // that.initDefaultDate(
                //   response.data.store.created_at.date,
                //   Number(response.data.store.validity)
                // );
                that.store.validity = response.data.store.validity.slice(0, 10);
              }
              that.store_state = response.data.store.operating_state;
              that.income = response.data.income;
              that.nearby = response.data.nearby;
              that.store_screen = response.data.store_screen;
              if (response.data.store.is_open_yun_horn && (response.data.store.is_open_yun_horn == 1)) {
                that.cloudHornStatus = true;
              } else {
                that.cloudHornStatus = false;
              }
              if (response.data.show_store_balance_award) {
                that.show_store_balance_award = response.data.show_store_balance_award;
              }
              if (response.data.store.is_open_zkc_pay && (response.data.store.is_open_zkc_pay == 1)) {
                that.zkzStatus = true;
              } else {
                that.zkzStatus = false;
              }
              this.is_share_chain = response.data.store?.is_share_chain;
              this.show_management = response.data.show_management;
              if(response.data.deduct_return){
                if(response.data.deduct_return.is_open == 1){
                  this.deduct_return_open = true;
                  this.deduct_return_name = response.data.deduct_return.name
                }
              }
              that.specify_show = response.data.specify_show;
              that.is_open_code = response.data.is_open_code;
              that.my_store_order = response.data.my_store_order;
              that.order_survey = response.data.order_survey;
              that.show_main_color_flag = true;
              that.show_store_balance_transfer = response.data.show_store_balance_transfer;
              let yz_basic_info = JSON.parse(localStorage.getItem("yz_basic_info"));
              if(yz_basic_info && yz_basic_info.love){
                this.loveName = yz_basic_info.love;
                this.is_share_chain_name = yz_basic_info?.plugin_setting?.share_chain?.plugin_name;
              }
            } else {
              if (response.data.url) {
                that.$dialog.confirm({ message: `${response.msg},是否跳转到门店申请页?`, confirmButtonText: '去申请'})
                .then(() => {
                  window.location.href = response.data.url;
                }).catch(() => {
                  that.$router.go(-1)
                });
              }else{
                that.$dialog.alert({
                  message: response.msg
                });
              }
              that.show_main_color_flag = true;

            }
          },
          function(response) {
            this.show_main_color_flag = true;
            // error callback
          }
        );
    },
    toRecord(){
      this.$router.push(
        this.fun.getUrl("baseRewardTemplate", {}, {
          api: "plugin.store-cashier.frontend.store.center.deduct-return.index",
        })
      );
    },
    // 续费按钮被点击的时候
    getCash() {
      this.$router.push(
        this.fun.getUrl("goodsorder", {}, {
          tag: "store",
          //  store_id: this.store.store_id,
          isCash: 1
        })
      );
    },
    // 显示续费按钮
    showCash() {
      $http
        .get("plugin.store-cashier.frontend.store.enter.enable")
        .then(res => {
          if (res.result === 1) {
            this.status = res.data.status;
          } else {
            this.$dialog.alert({
              message: res.msg
            });
          }
        });
    },
    initDefaultDate(tiem, n) {
      var d2 = new Date(tiem);
      d2.setFullYear(d2.getFullYear() + n);
      d2.setDate(d2.getDate() - 1);
      console.log(
        "开始时间：：",
        d2.toLocaleString("chinese", {
          hour12: false
        }).replace(/:d{1,2}$/, " ")
      );
      this.timeFn(
        d2.toLocaleString("chinese", {
          hour12: false
        }).replace(/:d{1,2}$/, " ")
      );
    },
    timeFn(d1) {
      //如果时间格式是正确的，那下面这一步转化时间格式就可以不用了
      var dateBegin = new Date(d1.replace(/-/g, "/")); //将-转化为/，使用new Date
      var dateEnd = new Date(); //获取当前时间
      var dateDiff = dateBegin.getTime() - dateEnd.getTime(); //时间差的毫秒数
      var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
      this.store.validity = dayDiff >= 0 ? dayDiff + "天 " : "已到期！";
    },
    storeStateSwitch() { //店铺状态切换
      $http
        .post("plugin.store-cashier.frontend.store.center.index.changeStatus")
        .then(res => {

          Toast(res.msg)
          if(res.result == 1){
            this.show = false;
            this.getStoreInfo()
          }
        });
    },
    store_popup(){
      this.show=true;
      this.store_state = this.store.operating_state;
    },
  },
  computed: {},
  activated() {

    this.show_main_color_flag = false;
    this.getStoreInfo();
    this.showCash();
    this.turnover_handle(2)
 
  },
  components: {
    cTitle
  }
};
